package com.hospital.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.hospital.dto.DignoseLogDto;
import com.hospital.dto.UserpayDto;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.hospital.dto.UserpayDto;
import com.hospital.entity.Admin;
import com.hospital.entity.User;
import com.hospital.service.intf.UserService;
import com.hospital.util.OssUtil;
import com.hospital.util.SessionUtil;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;

/**
 * @author fxy
 * @date 2022/8/28
 */
@RestController
@RequestMapping("/api/user/")
public class UserController {
    @Autowired
    private UserService service;

    //新增
    @PostMapping("add.do")
    public R add(User user){
        return service.add(user);
    }
    //修改
    @PostMapping("update.do")
    public R update(User user){
        return service.update(user);
    }
    //删除
    @GetMapping("del.do")
    public R del(int id){
        return service.delete(id);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(String name, int page, int limit){
        return service.selectPage(name, page, limit);
    }

    @RequestMapping("one.do")
    public R selectOne(User user, HttpSession session) throws IOException {
        System.out.println(service.selectOne(user,session));
        return service.selectOne(user,session);
    }
    @GetMapping("loginone.do")
    public R loginOut(HttpSession session){
        session.invalidate();
        return R.ok();
    }

    @GetMapping("updatesurplus.do")
    //修改用户的余额
    public R updatesuplus(UserpayDto userpayDto) throws ParseException {
        return service.updatesuplusTx(userpayDto);
    }
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=student-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<User> list=service.select();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),User.class).sheet(System.currentTimeMillis()+"").doWrite(list);

    }



    //数据导入
    @GetMapping("uploaduser.do")
    public R upload(MultipartFile file) throws IOException {
        //接收文件，获取文件内容
        //验证上传文件是否存在
        if(!file.isEmpty()){
            //解析Excel数据
            List<User> list = EasyExcel.read(file.getInputStream(), User.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            //mybatis实现批量新增
           if(service.addUserBatch(list)>0){
               return R.ok();
           }
        }
        return R.fail();
    }
    //批量删除数据
    @PostMapping("dels.do")
    public R delete(int[] ids){
        return service.del(ids);
    }

    //获取seesion中的用户对象
    @GetMapping("getuser.do")
    public R getSeesion(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        return R.ok(user);
    }

    //分页查询症状
    @GetMapping("pageDiclog.do")
    public PageBean pageDiclog(String symptom, int page, int limit,HttpServletRequest request){
        return service.selectdiglog(symptom, page, limit, SessionUtil.getSeesion(request).getId());
    }



    //查询,下载用户诊断记录
    @GetMapping("downdignoselog.do")
    public void downdignoselog(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=dignose-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<DignoseLogDto> list= service.selectdiglogall(null);
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(), DignoseLogDto.class).sheet(System.currentTimeMillis()+"").doWrite(list);

    }

    @GetMapping("pricetj.do")
    public R stateTj(){
        return service.priceTj();
    }

    //上传图片
    // springmvc 的 文件上传组件
    @PostMapping("/upload.do")
    public R uploadPic(MultipartFile file) throws IOException {
        //避免预检请求报错  这里有个 if  判断
        if(file!=null){
            //得到文件名
            String originalFilename = file.getOriginalFilename();
            // 将文件转为输入流
            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile(inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok(url);
        }
        return R.fail();
    }


    //修改用户头像
    @PostMapping("/updateimg.do")
    public R updateImg(HttpServletRequest request,String url){
        User user = (User) request.getSession().getAttribute("user");
        user.setImgurl(url);
        request.getSession().setAttribute("user",user);
        return service.updateimg(user.getId(),url);
    }



}
